package org.bees.cms.web.util.velocity.renderer;

import java.io.Serializable;

import org.bees.cms.web.util.Beans;

import net.sf.json.JSONObject;

public class ArrayRenderer extends AbstractRenderer {
	private String gap = "&nbsp;&nbsp;";
	private String left = "[";
	private String right = "]";

	@Override
	public void initialize(JSONObject json) throws Exception {
		super.initialize(json);
		gap = json == null ? gap : json.containsKey("gap") ? json.getString("gap") : gap;
		left = json == null ? left : json.containsKey("left") ? json.getString("left") : left;
		right = json == null ? right : json.containsKey("right") ? json.getString("right") : right;
	}

	@Override
	public String render(Serializable t) throws Exception {
		Object[] objects = (Object[]) Beans.get(t, property);
		StringBuilder builder = new StringBuilder();
		builder.append("<span id='" + id + "' class='" + clazz + "' style='" + style + "'>");
		for (int i = 0; objects != null && i < objects.length; i++) {
			builder.append(left).append(objects[i]).append(right).append(gap);
		}
		builder.append("</span>");
		return builder.toString();
	}

	@Override
	public void destroy() throws Exception {
		super.destroy();
		gap = null;
		left = null;
		right = null;
	}

	public String getGap() {
		return gap;
	}

	public void setGap(String gap) {
		this.gap = gap;
	}

	public String getLeft() {
		return left;
	}

	public void setLeft(String left) {
		this.left = left;
	}

	public String getRight() {
		return right;
	}

	public void setRight(String right) {
		this.right = right;
	}

}
